!  When I pulled the data from SAS I did not restrict the share code.
!    For BSV stuff sticking to share code 10 and 11 is necessary,
IMPLICIT NONE
DOUBLE PRECISION :: R,DR,DIV,at,lt,l1,p1,p2,up,tax,compso,PRC
INTEGER :: P,PC,D,SC,M,DD,DC,NP,HE,DIC,EX,DDD,lp,TM,IM,NG,PERMNO,PERMCO,errcod,i,status,ntc,nobs,gv,C,fyr
INTEGER :: LD,ND,ncomd,ir,S,LC,LS,SICCD,iostat
CHARACTER * 9 :: CUS
CHARACTER * 4 :: GAR,gar2,gar3
OPEN(unit=16, file='/Users/lamlap/CRSP/sas/mymerged_2_18_22.csv', STATUS ='OLD',READONLY,iostat=errcod)
OPEN(unit= 18, file='/Users/lamlap/BSV/2022/data/mergeSCA_2_8_19.dat', STATUS='replace')
! A reminder of how my current codes are structured to apply BSV:
!    Each row contains the permno, date, and return (from CRSP) for that month
!         plus the characteristics applicable to that month:
!             BtM lagged 6 - 18 months
!             Size lagged 2 months
!             Momentum (12 months, lagged 2 months)
!             beta on the CRSP VWMR beta from past 60 months
!             return from the same month last year
!             average return from the same month for the past 5 years
!
!    At this point, (and especially as I'm not doing anything special with dividends and/or splits), I am simply using the
!            return directly from CRSP (in 6 significant digits).
!
!    Irregularities that we see on input:
!                                         1) Multiple permnos for 1 permco.
!                                            Compute the size by multiplying each permno's prc * shrout on CRSP, and summing up.
!                                            Keep the compustat data for the permno that has the longest match.
!                                         2) Multiple rows with the same date because of distributions.
!                                         3) Possible missing compustat data and or switch back to earlier compustat date because of
!                                            change in fiscal year end.
LP=0
NTC=0
NOBS=0
READ (16,*,iostat=status) GAR
! PRINT 52; 52 format (' read first line of data')
ir=0
DO 
    P=-999
    PC=-999
    D=-999
    R=-999.d0
    PRC=-9999999.d0
    S=-999.d0
    C=-999
    M=-999
    dd=-999
    dc=-999
    dr=-999.d0
    he=-999
    dic=-999
    div=-999.d0
    ex=-999
    gv=-999
    ddd=-999
    fyr=-999
    cus='-99999999'
    at=-999.d0
    lt=-999.d0
    p1=-999.d0
    p2=-999.d0
    up=-999.d0
    tax=-999.d0
    compso=-999.d0
    siccd=-999
!     READ (16,*) P,PC,D,R,PRC,S,M,C,DD,DC,DR,HE,DIC,DIV,EX,GV,DDD,fyr,CUS,at,lt,p1,p2,up,tax,compso,lp
!  Note that with unformatted input I lose the last variable if it is ever missing.
    READ (16,*,iostat=status) P,PC,D,R,PRC,S,C,SICCD,M,DD,DC,DR,HE,DIC,DIV,EX,GV,DDD,fyr,CUS,at,lt,p1,p2,up,tax,compso
    IF (status /= 0) EXIT
    IF (D==LD) CYCLE           ! I'll get rid of the multiple entries on the same date in cases with multiple distributions in that month.
!    IF (P<-99) EXIT
    IF (C==10 .OR. C==11) THEN
       WRITE (18,1866) P,PC,D,R,PRC,S,C,SICCD,M,GV,DDD,fyr,CUS,at,lt,p1,p2,up,tax,compso
       LP=P
       LC=PC
       LS=S
!   We'll exclude the distribution information.
 1866 format (i5,1x,i5,1x,i8,1x,f11.6,1x,f14.5,1x,i10,2x,i4,1x,i4,1x,i8,1x,i8,1x,i8,1x,i4,1x,a9,7(1x,f12.3))
!             1     2      3      4        5        6     7     8     9    10    11    12    13    14- 20
       LD=D
    END IF
    IF (DC > -999 .AND. P==LP) THEN 
       PC=LC
       D=DD
       IF (D==LD) CYCLE
       IF (DR < -1.d0) then
          IF (HE==3) THEN
             DR=-0.5d0
          ELSE
             DR=-0.3d0
          END IF
       END IF
       R=DR        !  This is where we use the delisting return rule.
       S=LS
       WRITE (18,1866) P,PC,D,R,PRC,S,C,SICCD,M,GV,DDD,fyr,CUS,at,lt,p1,p2,up,tax,compso
       LD=D
    END IF
END DO
STOP; END
